package com.calclab.hablar.rooms.client.settings;

import static com.calclab.hablar.rooms.client.HablarRooms.i18n;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.calclab.emite.core.client.xmpp.session.Session;
import com.calclab.emite.core.client.xmpp.stanzas.XmppURI;
import com.calclab.emite.xep.muc.client.Occupant;
import com.calclab.emite.xep.muc.client.Room;
import com.calclab.emite.xep.storage.client.PrivateStorageManager;
import com.calclab.hablar.core.client.mvp.HablarEventBus;
import com.calclab.hablar.core.client.page.PagePresenter;
import com.calclab.hablar.core.client.validators.TextValidator;
import com.calclab.hablar.core.client.validators.Validators;
import com.calclab.hablar.rooms.client.RoomName;
import com.calclab.suco.client.Suco;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class SetRoomPresenter extends PagePresenter<SetRoomDisplay>{

    private static final String TYPE = "SetRoom";
    
    private final PrivateStorageManager storageManager;
    
    private final HashMap<String, SelectMediaItemPresenter> itemsByUri;
    protected TextValidator roomNameValidator;
    
    private final SetRoomDisplay display;
    
    private Room room;
    
    public SetRoomPresenter(final HablarEventBus eventBus, final SetRoomDisplay display){
	super(TYPE, eventBus, display);
	storageManager = Suco.get(PrivateStorageManager.class);
	itemsByUri = new HashMap<String, SelectMediaItemPresenter>();
	this.display = display;
	
	display.getCancel().addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(final ClickEvent event) {
		requestVisibility(Visibility.hidden);
	    }
	});

	display.getConfirm().addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(final ClickEvent event) {
		onAccept();
		requestVisibility(Visibility.notFocused);
	    }
	});

	roomNameValidator = new TextValidator(display.getRoomNameKeys(), display.getRoomName(), display
		.getRoomNameError(), display.getConfirmEnabled());
	roomNameValidator.add(Validators.notEmpty(i18n().emptyGroupChatName()));
	// roomNameValidator.add(Validators.hasNotSpaces(i18n().spacesInRoomName()));
	roomNameValidator.add(Validators.isValidRoomName(i18n().notValidGroupChatName()));
    }
   

    public void setRoom(final Room room) {
	this.room = room;
    }


    protected void onAccept() {
	final Session session = Suco.get(Session.class);
	final XmppURI user = session.getCurrentUser();
	final String reasonText = display.getDescription().getText();
	for (final SelectMediaItemPresenter selectItem : getItems()) {
	    if (selectItem.isSelected()) {
//		room.sendInvitationTo(selectItem.getItem().getJID(), reasonText);
	    }
	}
    }

    
    protected void onPageOpen() {
	display.setPageTitle(i18n().groupSettingsTitle());
	
	final String roomName = RoomName.decode(room.getURI().toString());
	display.getRoomName().setText(roomName);
	display.setRoomNameEnabled(false);
	
	List<MediaItem> mediaItems = new ArrayList<MediaItem>();
	mediaItems.add(new MediaItem("1","im","best001@im.com"));
	mediaItems.add(new MediaItem("2","email","best001@instant.com"));
	setItems(mediaItems, true, true);
	for (final Occupant occupant : room.getOccupants()) {
	    setItem(occupant.getURI().getJID(), false, true);
	}
    }
    
    
    public Collection<SelectMediaItemPresenter> getItems() {
	return itemsByUri.values();
    }

    public void setItem(final XmppURI uri, final boolean enabled, final boolean selected) {
	final SelectMediaItemPresenter item = itemsByUri.get(uri);
	if (item != null) {
	    item.setEnabled(enabled);
	    item.setSelected(selected);
	}
    }

    public void setItems(final Collection<MediaItem> items, final boolean enabled, final boolean selected) {
	display.clearList();
	itemsByUri.clear();
	for (final MediaItem item : items) {
	    createItem(item, enabled, selected);
	}
    }

    protected void createItem(final MediaItem item, final boolean selectable, final boolean selected) {
	final SelectMediaItemDisplay itemDisplay = display.createItem();
	final SelectMediaItemPresenter selectItem = new SelectMediaItemPresenter(item, itemDisplay, selectable);
	selectItem.setSelected(selected);
	display.addItem(itemDisplay);
	itemsByUri.put(item.getJid(), selectItem);
    }

    protected SelectMediaItemPresenter getItem(final String jid) {
	return itemsByUri.get(jid);
    }
    
    @Override
    protected void onBeforeFocus() {
	onPageOpen();
    }
}
